package org.lex.io.filesystem;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileView;

import org.lex.swing.Swings;


// simple file view, may be faster than DefaultFileView
public class SimpleFileView extends FileView {
	private static Icon FILE_ICON = Swings.fileIcon();
	private static Icon DIRECTORY_ICON = Swings.directoryIcon();
	private static String FILE_DESCRIPTION = "File";
	private static String DIRECTORY_DESCRIPTION = "Folder";

	private static final FileSystemView fsv = FileSystemView.getFileSystemView();

	static {
		UIManager.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				if (e.getPropertyName().equals("lookAndFeel")) {
					FILE_ICON = Swings.fileIcon();
					DIRECTORY_ICON = Swings.directoryIcon();
				}
			}
		});
	}

	public String getName(File f) {
		return fsv.getSystemDisplayName(f);
	}

	public Icon getIcon(File f) {
		if (null == f)
			return null;
		return true == f.isFile() ? FILE_ICON : DIRECTORY_ICON;
	}

	public String getDescription(File f) {
		return this.getTypeDescription(f);
	}

	public String getTypeDescription(File f) {
		if (true == f.isDirectory())
			return DIRECTORY_DESCRIPTION;
		String name = f.getName();
		int index = name.lastIndexOf('.');
		if (-1 == index)
			return FILE_DESCRIPTION;
		return name.substring(index + 1) + " " + FILE_DESCRIPTION;
	}
}
